package com.example.ccod.nacos.gateway.controller;

import com.ccod.nacos.goods.facade.GoodsDto;
import com.ccod.nacos.order.model.OrderDTO;
import com.example.ccod.nacos.gateway.client.GoodsFacadeClient;
import com.example.ccod.nacos.gateway.client.OrderFacadeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: ccod
 * @data: 2020/12/27 13:32
 **/
@RestController
public class GatewayController {


    @Autowired
    private GoodsFacadeClient goodsFacadeClient;

    @Autowired
    private OrderFacadeClient orderFacadeClient;

    @Autowired
    private SeataService seataService;

    @GetMapping
    public String testSeata(String enable){
        return seataService.testSeata(enable);
    }

    @GetMapping("allGoods")
    public R<List<GoodsDto>> searchGoods(){
        return R.success(goodsFacadeClient.searchGoods());
    }

    @GetMapping("searchOrder/{orderId}")
    public R<OrderDTO> searchOrder(@PathVariable String orderId){
        return R.success(orderFacadeClient.searchOrder(orderId));
    }

    @PostMapping("addGoods")
    public R searchOrder( GoodsDto dto){
        return goodsFacadeClient.addGoods(dto) ? R.success() : R.fail();
    }

    @PostMapping("createOrder")
    public R searchOrder( OrderDTO dto){
        return orderFacadeClient.createOrder(dto) ? R.success() : R.fail();
    }


    public static class R<T>{

        private boolean success;

        private String resultCode;

        private T result;

        public static <T> R<T> success(T result){
            R<T> r = new R();
            r.success = true;
            r.resultCode = "SUCCESS";
            r.result = result;
            return r;
        }

        public static <T> R<T> success(){
            R<T> r = new R();
            r.success = true;
            r.resultCode = "SUCCESS";
            return r;
        }

        public static <T> R<T> fail(){
            R<T> r = new R();
            r.success = false;
            r.resultCode = "SYSTEM_ERROR";
            return r;
        }

        public static <T> R<T> fail(String code){
            R<T> r = new R();
            r.success = false;
            r.resultCode = code;
            return r;
        }

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public String getResultCode() {
            return resultCode;
        }

        public void setResultCode(String resultCode) {
            this.resultCode = resultCode;
        }

        public T getResult() {
            return result;
        }

        public void setResult(T result) {
            this.result = result;
        }
    }


}
